package com.yc.cloud.admin.controller;

import com.yc.cloud.admin.dto.HwRegionNode;
import com.yc.cloud.admin.dto.request.HwRegionCreateReqDTO;
import com.yc.cloud.admin.dto.request.HwRegionQueryReqDTO;
import com.yc.cloud.admin.dto.request.HwRegionUpdateReqDTO;
import com.yc.cloud.admin.service.HwRegionService;
import com.yc.cloud.admin.vo.HwRegionVo;
import com.yc.cloud.common.basic.api.ResponseResult;
import com.yc.cloud.common.orm.CommonPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 硬件区域管理表 前端控制器
 * </p>
 *
 * @author kinggu
 * @since 2025-09-29
 */
@Slf4j
@RestController
@Api(tags = "硬件区域管理")
@RequestMapping("/hw-regions")
public class HwRegionController {

    @Resource
    private HwRegionService hwRegionService;

    @ApiOperation("创建区域")
    @PostMapping
    public ResponseResult<HwRegionVo> create(@Validated @RequestBody HwRegionCreateReqDTO createReqDTO) {
        HwRegionVo newVo = hwRegionService.create(createReqDTO);
        if (newVo != null) {
            return ResponseResult.success(newVo);
        }
        return ResponseResult.failed("创建失败");
    }

    @ApiOperation("更新区域")
    @PutMapping("/{id}")
    public ResponseResult<HwRegionVo> update(@PathVariable("id") Long id, @Validated @RequestBody HwRegionUpdateReqDTO updateReqDTO) {
        updateReqDTO.setId(id);
        HwRegionVo updatedVo = hwRegionService.update(updateReqDTO);
        if (updatedVo != null) {
            return ResponseResult.success(updatedVo);
        }
        return ResponseResult.failed("更新失败");
    }

    @ApiOperation("删除区域")
    @DeleteMapping("/{id}")
    public ResponseResult<Boolean> delete(@PathVariable("id") Long id) {
        try {
            boolean success = hwRegionService.delete(id);
            if (success) {
                return ResponseResult.success(true);
            }
            return ResponseResult.failed("删除失败");
        } catch (RuntimeException e) {
            return ResponseResult.failed(e.getMessage());
        }
    }

    @ApiOperation("获取区域详情")
    @GetMapping("/{id}")
    public ResponseResult<HwRegionVo> getById(@PathVariable("id") Long id) {
        return ResponseResult.success(hwRegionService.getVOById(id));
    }

    @ApiOperation("分页查询区域列表")
    @GetMapping
    public ResponseResult<CommonPage<HwRegionVo>> list(@Validated HwRegionQueryReqDTO queryReqDTO) {
        return ResponseResult.success(hwRegionService.list(queryReqDTO));
    }

    @ApiOperation("获取区域树状列表")
    @GetMapping("/tree")
    public ResponseResult<List<HwRegionNode>> treeList() {
        return ResponseResult.success(hwRegionService.treeList());
    }

    @ApiOperation("根据层级查询区域列表")
    @GetMapping("/level/{level}")
    public ResponseResult<List<HwRegionVo>> getByLevel(@PathVariable("level") @ApiParam("行政级别") Integer level) {
        return ResponseResult.success(hwRegionService.getByLevel(level));
    }

    @ApiOperation("根据父级ID查询子区域列表")
    @GetMapping("/parent/{parentId}")
    public ResponseResult<List<HwRegionVo>> getByParentId(@PathVariable("parentId") @ApiParam("父级区域ID") Long parentId) {
        return ResponseResult.success(hwRegionService.getByParentId(parentId));
    }

    @ApiOperation("获取区域的所有子区域ID（递归）")
    @GetMapping("/{id}/children")
    public ResponseResult<List<Long>> getChildrenIds(@PathVariable("id") @ApiParam("区域ID") Long id) {
        return ResponseResult.success(hwRegionService.getChildrenIds(id));
    }
}